<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteTemplates()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input  id="selectByName">
									<button class="btn btn-default" onclick="selectByName()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="templateShow">

			                      </tbody>
			                  </table>
							<!--数据列表/-->
							<form id="page">

								<a href="javascript:toPage(1)">首页</a>
								<a href="javascript:toPage(2)">上一页</a>
								第<input type="text"  id="pageNum" size="1px" readonly="readonly">页/
								共<input type="text" id="pages" size="1px" disabled="disabled">页/
								共<input type="text" id="total" size="1px" disabled="disabled">条
								<a href="javascript:toPage(3)">下一页</a>
								<a href="javascript:toPage(4)">尾页</a>
							</form>
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                    
	           
					    
                                
<!-- 编辑窗口 新增 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" placeholder="商品类型" id="tname">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td id="brands">
						<!--		multiple属性 下拉菜单可以多选				-->

		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td id="specs">

		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性" onclick="addOption()"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="add">
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除" onclick="deleteOptions(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr id="addOption">
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" class="btn btn-default" title="删除"  onclick="deleteOptions(this)"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>
           
		      		</td>
		      	</tr>
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addTemplate()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 编辑窗口 修改 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3>商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td><input  class="form-control"  type="hidden" placeholder="商品类型" id="tid">  </td>
					  </tr>
					  <tr>
						  <td>商品类型</td>
						  <td><input  class="form-control" placeholder="商品类型" id="tname2">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td id="brands2">
							  <!--		multiple属性 下拉菜单可以多选				-->

						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td id="specs2">

						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" class="btn btn-default" title="新增扩展属性" onclick="updateOption()"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>
								  <tbody id="add2">


								  </tbody>
							  </table>

						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTemplate()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
<script>
	//新增规格选项
	function addOption() {
		var cloneOption = $("#addOption").clone();
		$("#add").append(cloneOption);
		$(cloneOption).find(":input").val('');
	}
	//修改规格选项
	function updateOption() {
		var cloneOption = $("#addOption2").clone();
		$("#add2").append(cloneOption);
		$(cloneOption).find(":input").val('');
	}
	//删除规格选项
	function deleteOptions(now){
		$(now).parent().parent().remove();
	}
	$(function (){
		listTemplate()
		listBrands()
		listSpec()
		listBrands2()
		listSpec2()
		$("#brandSelect").select2();
		$("#specSelect").select2();
		$("#brandSelect2").select2();
		$("#specSelect2").select2();
	})
	function listTemplate(num){
		$.ajax({
			url: "/template/listTemplate",
			data: {pageNum:num,name:$("#selectByName").val(),pageSize:2},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var templates = result.data.rows;
					for (var i = 0; i < templates.length; i++) {
						html += '<tr>'
						html += '<td><input type="checkbox" value="'+ templates[i].id +'"></td>'
						html += '<td>' + templates[i].id + '</td>'
						html += '<td>' + templates[i].name + '</td>'
						var templatesJson = JSON.parse(templates[i].brandIds);
						var bstr = ''
						for (var a = 0; a < templatesJson.length; a++) {
							bstr += ',' + templatesJson[a].text
						}
						html += '<td> ' + bstr.substring(1) + ' </td>'

						var specIdsJson = JSON.parse(templates[i].specIds);
						var sstr = ''
						for (var b = 0; b < specIdsJson.length; b++) {
							sstr += ',' + specIdsJson[b].text;
						}
						html += '<td>' + sstr.substring(1) + '</td>'
						var customJson = JSON.parse(templates[i].customAttributeItems)
						var kstr = ''
						for (var c = 0; c < customJson.length; c++) {
							kstr += ',' + customJson[c].text;
						}
						html += '<td>' + kstr.substring(1) +'</td>'
						kstr = ''
						html += '<td className="text-center">'
						html += '<button type="button" className="btn bg-olive btn-xs" data-toggle="modal" onclick="toUpdate('+ templates[i].id +')" data-target="#updateModal">修改</button>'
						html += '</td>'
						'</tr>'
					}
					$("#templateShow").html(html);
					// 分页
					$("#pageNum").val(result.data.pageNum);
					$("#pages").val(result.data.pages);
					$("#total").val(result.data.total);
				}else{
					$("#templateShow").html("<tr><td>" + result.message + "</td></td></tr>");
				}

			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	//模糊查询方法
	function selectByName(){
		listTemplate()
	}
	//分页
	function toPage(id){
		var num = parseInt($("#pageNum").val());
		var pages = parseInt($("#pages").val());
		var p = num;
		if (id == 1) {
			p = 1;
			$("#pageNum").val(1);
		}
		if (id == 2){
			if (num == 1) {
				alert("已经到达首页了");
				return;
			}
			p = num - 1;
			$("#pageNum").val(num - 1);
		}
		if (id == 3){
			if (num == pages) {
				alert("已经到达尾页了");
				return;
			}
			p = num + 1;
			$("#pageNum").val(num + 1);
		}
		if (id==4){
			p = pages;
			$("#pageNum").val(pages);
		}
		listTemplate(p);
	}
	function listBrands(){
		$.ajax({
			url: "/template/listBrand",
			//data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var listBrand = result.data;
					html +='<select id="brandSelect" style="width:100%" multiple>'
					$(listBrand).each(function (i,e){
						html += '<option value="' + e.id +'">'
						html +=	e.name
						html += '</option>'
					})
					html += '</select>'
					$("#brands").html(html);
					$("#brands2").html(html);
				}else{
					$("#brands").html("<tr><td>" + result.message + "</td></td></tr>");
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function listSpec(){
		$.ajax({
			url: "/template/listSpec",
			//data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var listBrand = result.data;
					html +='<select id="specSelect" style="width:100%" multiple>'
					$(listBrand).each(function (i,e){
						html += '<option value="' + e.id +'">'
						html +=	e.specName
						html += '</option>'
					})
					html += '</select>'
					$("#specs").html(html);
					$("#specs2").html(html);
				}else{
					$("#specs").html("<tr><td>" + result.message + "</td></td></tr>");
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function listBrands2(){
		$.ajax({
			url: "/template/listBrand",
			//data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var listBrand = result.data;
					html +='<select id="brandSelect2" style="width:100%" multiple>'
					$(listBrand).each(function (i,e){
						html += '<option value="' + e.id +'">'
						html +=	e.name
						html += '</option>'
					})
					html += '</select>'
					$("#brands2").html(html);
				}else{
					$("#brands2").html("<tr><td>" + result.message + "</td></td></tr>");
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function listSpec2(){
		$.ajax({
			url: "/template/listSpec",
			//data: {pageNum:num,name:$("#selectByName").val()},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					var listBrand = result.data;
					html +='<select id="specSelect2" style="width:100%" multiple>'
					$(listBrand).each(function (i,e){
						html += '<option value="' + e.id +'">'
						html +=	e.specName
						html += '</option>'
					})
					html += '</select>'
					$("#specs2").html(html);
				}else{
					$("#specs2").html("<tr><td>" + result.message + "</td></td></tr>");
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function addTemplate(){
		var brandList = $("#brandSelect").select2('data')
		var specList = $("#specSelect").select2('data')
		var tname = $("#tname").val()
		var brandArr = []
		var specArr = []
		var newArr = []
		$(brandList).each(function (i,e){
			var b = {id:e.id,text:e.text}
			brandArr.push(b)
		})
		$(specList).each(function (i,e){
			var b = {id:e.id,text:e.text}
			specArr.push(b)
		})
		var specJson = JSON.stringify(specArr)
		var brandJson = JSON.stringify(brandArr)

		var newOption = $("#add").find("tr") //获取整个表格的tr
		$(newOption).each(function (i,e){  //循环tr
			var td = $(e).find("td"); //td = tr中的td
			var newId = i
			var newName = $(td[1]).find("input").val();
			var options = {id:newId,text:newName};//按照json进行赋值
			newArr.push(options);//添加到数组
		})
		var newJson = JSON.stringify(newArr);
		$.ajax({
			url: "/template/addTemplate",
			data: {name:tname,specIds:specJson,brandIds:brandJson,customAttributeItems:newJson},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == 666){
					alert(result.message)
					listTemplate()
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function toUpdate(id){
		var bIds = []
		var sIds = []
		$.ajax({
			url: "/template/toUpdate",
			data: {id:id},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				var html = ""
				if(result.code == 666){
					$("#tid").val(result.data.id)
					$("#tname2").val(result.data.name)
					var brands =  result.data.brandIds
					var brandJson = JSON.parse(brands)
					$(brandJson).each(function (i,e){
						bIds.push(e.id)
					})
					$("#brandSelect2").val(bIds).trigger('change')
					var specs =  result.data.specIds
					var specJson = JSON.parse(specs)
					$(specJson).each(function (i,e){
						sIds.push(e.id)
					})
					$("#specSelect2").val(sIds).trigger('change')
					var news = result.data.customAttributeItems;
					var newJson = JSON.parse(news);
					console.log(newJson)
					$(newJson).each(function (i,e){
						html += '<tr id="addOption2">'
						html += '<td><input type="checkbox" className="icheckbox_square-blue"></td>'
						html += '<td><input className="form-control" placeholder="属性名称" value="'+ e.text +'"></td>'
						html += '<td>'
						html += '<button type="button" className="btn btn-default" title="删除" onClick="deleteOptions(this)"><i className="fa fa-trash-o"></i>删除</button>'
						html += '</td>'
						html += '</tr>'
					})
					$("#add2").html(html)
				}else{

				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function updateTemplate(){
		var id = $("#tid").val()
		var name = $("#tname2").val()
		var brandList = $("#brandSelect2").select2('data')
		var specList = $("#specSelect2").select2('data')
		var brandArr = []
		var specArr = []
		var newArr = []
		$(brandList).each(function (i,e){
			var b = {id:e.id,text:e.text}
			brandArr.push(b)
		})
		$(specList).each(function (i,e){
			var b = {id:e.id,text:e.text}
			specArr.push(b)
		})
		var specJson = JSON.stringify(specArr)
		var brandJson = JSON.stringify(brandArr)
		console.log(brandJson)
		var newOption = $("#add2").find("tr") //获取整个表格的tr
		$(newOption).each(function (i,e){  //循环tr
			var td = $(e).find("td"); //td = tr中的td
			var newId = i + 1
			var newName = $(td[1]).find("input").val();
			var options = {id:newId,text:newName};//按照json进行赋值
			newArr.push(options);//添加到数组
		})
		var newJson = JSON.stringify(newArr);
		$.ajax({
			url: "/template/updateTemplate",
			data: {id:id,name:name,specIds:specJson,brandIds:brandJson,customAttributeItems:newJson},
			type: "get",
			dataType: "json",
			async:false,
			success: function (result) {
				if(result.code == 666){
					alert(result.message)
					listTemplate()
				}else{
					alert(result.message)
				}
			},
			error: function (){
				alert("系统异常")
			}
		})
	}
	function deleteTemplates(){
		if(confirm("是否删除")){
			var ids = []  //定义一个数组
			$("[type=checkbox]").each(function (i, e) {   //获取所有复选框的value,循环
				if ($(e).prop("checked")) {   //判断是否被选中
					ids.push($(e).val())  //选中就添加到数组中
				}
			})
			if (ids.length <= 0){
				alert("请选择要删除的数据")
				return
			}
			$.ajax({
				url: "/template/deleteTemplates?ids=" + ids,
				// data: {ids:ids},
				type: "get",
				dataType: "json",
				async: false,
				success: function (result) {
					if (result.code == "666") {
						alert(result.message)
						listTemplate();
					} else {
						alert(result.message)
					}
				},
				error: function (){
					alert("系统异常")
				}
			})
		}
	}
</script>
</html>